//
//  RegisterViewController.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-4.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddRegisterViewController.h"
#import "JddCommonFunction.h"
#import "JddGlobalParameter.h"
#import "JddDataParser.h"
#import "JddError.h"
#import "JddUserProfile.h"
#import "JddViewControllerManager.h"

@interface JddRegisterViewController ()

@end

@implementation JddRegisterViewController

@synthesize userNameTextField;
@synthesize passwordTextField;
@synthesize confirmPwdTextField;
@synthesize submitButton;
@synthesize bgScrollView;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddRegisterViewController);

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    tapGestureRecognizer.delegate = self;
    [self.bgScrollView addGestureRecognizer:tapGestureRecognizer];
    
    [self.bgScrollView setContentOffset:CGPointMake(0,100)
//                                                    submitButton.frame.size.height + submitButton.frame.origin.y - userNameTextField.frame.origin.y - userNameTextField.frame.size.height)
                               animated:YES];
    
    
    return YES;
}

-(IBAction)editPassword:(id)sender{
    [passwordTextField becomeFirstResponder];
}

-(IBAction)editPassword2:(id)sender{
    [confirmPwdTextField becomeFirstResponder];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:submitButton]) {
        return NO;
    }
    
    return YES;
}

- (IBAction)handleSingleTapGesture:(UITapGestureRecognizer*)sender{
    [userNameTextField resignFirstResponder];
    [passwordTextField resignFirstResponder];
    [confirmPwdTextField resignFirstResponder];
    
    [self.bgScrollView removeGestureRecognizer:sender];
    [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void)RegisterOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        if ([JddDataParser parseRegisterResult:result]) {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
            [self login];
        }else{
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
    }else{
        [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
    }
}

-(void)login
{
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    
    JddLogonOperation *operation = [[JddLogonOperation alloc] init];
    operation.delegate = self;
    operation.userName = userNameTextField.text;
    operation.userPassword = passwordTextField.text;
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}

- (void)LogonOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        NSString *uId = [JddDataParser parseLoginResult:result];
        if (uId.length == 0) {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }else{
            [JddUserProfile sharedInstance].name = userNameTextField.text;
            [JddUserProfile sharedInstance].password = passwordTextField.text;
            [JddUserProfile sharedInstance].userId = uId;
            
            [[JddGlobalParameter sharedInstance].dictionary setObject:userNameTextField.text forKey:GLOBAL_PARAMETER_USER];
            [[JddGlobalParameter sharedInstance].dictionary setObject:passwordTextField.text forKey:GLOBAL_PARAMETER_PASSWORD];
            [[JddGlobalParameter sharedInstance].dictionary setObject:[NSNumber numberWithBool:NO] forKey:GLOBAL_PARAMETER_SAVE_PASSWORD];
            [[JddGlobalParameter sharedInstance].dictionary setObject:uId forKey:GLOBAL_PARAMETER_USER_ID];
            
            [[JddGlobalParameter sharedInstance] save];
            
            
            //NSLog(@"==================%d",self.navigationController.viewControllers.count);
            if (self.navigationController.viewControllers.count <=4) {
                [self.navigationController popToViewController:[JddUserCenterViewController sharedInstance] animated:YES];
            }else if(self.navigationController.viewControllers.count ==5){
                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
            }else if(self.navigationController.viewControllers.count ==7){
                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:4] animated:YES];
            }else{
                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:3] animated:YES];
            }
            
        }
        
    }
    else {
        [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
    }
}


- (IBAction)registerButtonClicked:(id)sender{
    if (!self.checkBoxBtn.selected) {
        [JddCommonFunction messageBoxWithMessage:REGISTER_NOT_READ];
    }
    else if ([userNameTextField.text length] == 0) {
        [JddCommonFunction messageBoxWithMessage:PLEASE_INPUT_USER_NAME];
    }else if(passwordTextField.text.length < 6){
        [JddCommonFunction messageBoxWithMessage:PLEASE_INPUT_PASSWORD_S];
    }
    else if(passwordTextField.text.length > 15){
        [JddCommonFunction messageBoxWithMessage:PLEASE_INPUT_PASSWORD_F];
    }
    else if (![passwordTextField.text isEqualToString:confirmPwdTextField.text]) {
        [JddCommonFunction messageBoxWithMessage:PASSWORD_CONFIRM_FAILED];
    }else{
        [userNameTextField resignFirstResponder];
        [passwordTextField resignFirstResponder];
        [confirmPwdTextField resignFirstResponder];
        
        [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        [JddCommonFunction setWaitingViewHidden:NO fromController:self];
        JddRegisterOperation *operation = [[JddRegisterOperation alloc] init];
        operation.delegate = self;
        operation.userName = userNameTextField.text;
        operation.userPassword = passwordTextField.text;
        operation.cmdID = @"0";
        [[[JddGlobalParameter sharedInstance] operationQueue] addOperation:operation];
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return  YES;
}// clear button事件,返回NO过滤之
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}// 'return' key事件.返回NO过滤之

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    [JddCommonFunction setViewBackgroundWithImageName:@"jdd_default_bg01" AndView:self.view];
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:PAGE_REGISTER];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    //self.navigationItem.hidesBackButton = YES;
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"RegisterView"];

    userNameTextField.text= @"";
    passwordTextField.text= @"";
    confirmPwdTextField.text= @"";
    [userNameTextField resignFirstResponder];
    [passwordTextField resignFirstResponder];
    [confirmPwdTextField resignFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"RegisterView"];
}
-(IBAction)readProtocol:(id)sender
{
    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_READ_REGISTER_PROTOCOL
     
                                                         fromController:self
                                                             withObject:nil
                                                            withObject2:nil];
    
}

@end
